Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
JavaScript. 10%5 === 0 --> false?

Please help to find mistakes in my code:

function multipleOfIndex(array) {
    return array.forEach( item => console.log(item%array.indexOf(item) === 0) )
}
    
console.log(multipleOfIndex([68, -1, 1, -7, 10, 10])) // false, true, false, false, false, false 

obviously, I was expecting

console.log(multipleOfIndex([68, -1, 1, -7, 10, 10])) // false, true, false, false, false, true 

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is relying on indexOf when more than one value is the same in the array - array.indexOf(10) is always 4, see the extended log here:

function multipleOfIndex(array) {
    return array.forEach( item => console.log(item,array.indexOf(item),item%array.indexOf(item) === 0) )
}
    
console.log(multipleOfIndex([68, -1, 1, -7, 10, 10])) // false, true, false, false, false, false

The solution is to use the forEach index parameter (2nd one) https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

function multipleOfIndex(array) {
    return array.forEach( (item,idx) => console.log(item%idx === 0) )
}
    
console.log(multipleOfIndex([68, -1, 1, -7, 10, 10])) // false, true, false, false, false, false

about 4 years ago · Juan Pablo Isaza Denunciar

0

The [indexOf] method returns the first index of a given element that can be found in the array. If it does not exist, it returns - 1. So the [indexOf] value of the last 10 is 4.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda